#!/usr/bin/env python3.8
# -*- coding: utf-8 -*-
# @Author     :
# @Description: 导出csv,且可配置
import datetime
import time
import pandas as pd
import psycopg2
from Connection_all import *
from log_ex2 import *

sys.stdout = Logger(sys.stdout)
""" 取当前日期、月份、天、小时 """
time_new = datetime.datetime.now().strftime("%Y%m%d")
month_new = datetime.datetime.now().month
day_new = datetime.datetime.now().day
hour_judge = datetime.datetime.now().hour
week_time = datetime.datetime.now().weekday() + 1

""" 取当前时间的前一天日期 """
before_time = datetime.datetime.now() + datetime.timedelta(days=-1)
before_time_format = before_time.strftime("%Y%m%d")

#时间
now = time.strftime("%Y-%m-%d-%H-%M-%S")
now2 = format(now)



"""定义schemas参数--共用目录 """
fix_data_frame = read_canshu_data(os.path.join(abs_path_canshu_file, "database.txt"))
schemas=fix_data_frame.iloc[0, 5]
#table=fix_data_frame.iloc[0, 1]


""" 连接naviacat数据库 """
fix_data_frame = read_canshu_data(os.path.join(abs_path_canshu_file, "database.txt"))
link_to_navicat_db = psycopg2.connect(database=fix_data_frame.iloc[0, 0], user=fix_data_frame.iloc[0, 1],
                                      password=fix_data_frame.iloc[0, 2], host=fix_data_frame.iloc[0, 3],
                                      port=fix_data_frame.iloc[0, 4])
""" 创建一个游标 """
cur = link_to_navicat_db.cursor()
#print (schemas+table)
print("当前时间为：", datetime.datetime.now())
num = 0

for table in open(r".\config\mtable.txt"):
    num = table.strip('\n')
    sql_seq_app = f'SELECT * FROM '+schemas+'.'+table+';'
    df = pd.read_sql_query(sql_seq_app,con=link_to_navicat_db)
    print(table, end='')
    #df.to_csv(r'./export/%s%s.csv' %(num,now))
    df.to_csv(r'./export/%s.csv' % (num),index=False)



#wai_zao_data = query_mpp_data(sql_seq_zao1, sql_seq_zao2, sql_seq_zao3, cur)

link_to_navicat_db.close()


